var express = require('express');
var router = express.Router();
var routerService = require("../service/routerService")
var typeService = require("../service/typeService")
var typeRouterService = require("../service/typeRouterService")


/* GET home page. */
router.get('/query', async function (req, res, next) {
    let result = await routerService.query({ type: 1 });
    let result2 = await typeService.query({ type: { "$ne": 0 } })
    //请求数据
    res.json({
        code: 200,
        msg: "菜单的查询接口",
        typeList: result2,
        result,
    })
});
//下拉框改变
router.get("/typerouterquery", async (req, res, next) => {
    let { typeid } = req.query;
    let result = await typeRouterService.query({ typeid });
    res.json({
        code: 200,
        data: result
    })

})
router.post("/add", async (req, res, next) => {
    let { name, path, type, com } = req.body;
    let result = await routerService.add({
        name,
        path,
        type,
        com
    })
    res.json({
        code: 200,
        msg: "新增成功",
        data: result
    })
})

router.post("/typerouteradd", async (req, res, next) => {
    let { typeid, routerlist } = req.body;
    let list = routerlist.map((item) => {
        return {
            typeid,
            routerid: item._id,
            name: item.name,
            path: item.path,
            com: item.com
        }
    })

    //批量删除
    await typeRouterService.delMany({ typeid });

    let result = await typeRouterService.insertMany(list);
    res.json({
        code: 200,
        data: result,
    })
})


module.exports = router;